﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;
using DevExpress.Web.ASPxGridView;

namespace TravelLink.WebApp.Admin
{
    public partial class AdminCompany : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckResponse("2001");
            if (!this.IsPostBack)
            {
                adefrom.Value = DateTime.Now.AddYears(-1);
                adeto.Value = DateTime.Now;
            }
        }

        protected void abtnSearch_Click(object sender, EventArgs e)
        {
            if (this.atbkey.Text.Trim() == string.Empty)
            {
                this.LinqDataSource1.Where = @"CreateDate>=@datefrom&&CreateDate<=@dateto";
                this.LinqDataSource1.WhereParameters.Add("datefrom",System.Data.DbType.DateTime,this.adefrom.Date.ToString("yyyy-MM-dd 00:00:00"));
                this.LinqDataSource1.WhereParameters.Add("dateto", System.Data.DbType.DateTime, this.adeto.Date.ToString("yyyy-MM-dd 23:59:59"));
            }
            else
            {
                this.LinqDataSource1.Where = @"(Email+CompanyName+CompanyNameEN+CompanyTel+CompanyFax+ContactTel+ContactMobile+ContactPerson+Brand+EBrand).Contains(@key)";
                this.LinqDataSource1.WhereParameters.Add("key", this.atbkey.Text);
            }
        }

        protected void abtnuploadclogo_Click(object sender, EventArgs e)
        {
            TL_Company obj = this.ASPxGridView1.GetRow(this.ASPxGridView1.EditingRowVisibleIndex) as TL_Company;
            FileUpload fu = (FileUpload)this.ASPxGridView1.FindEditFormTemplateControl("FileUploadc");
            Image img = (Image)this.ASPxGridView1.FindEditFormTemplateControl("imgc");            
            string fullFileName = fu.PostedFile.FileName;
            string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
            string typeFileName = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
            if (typeFileName == "jpg" || typeFileName == "gif" || typeFileName == "png" || typeFileName == "bmp")
            {
                string fn =obj.CompanyCode.ToString() + "." + typeFileName;
                string path = Server.MapPath(@"..\Images\Company") + "\\" + fn;
                System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath(@"..\Images\Company") + "\\" + obj.CompanyLogo);
                if (fi.Exists)
                {
                    fi.Delete();
                }
                obj.CompanyLogo = fn;
                var dc = DataContextFactory.Create();
                var n= dc.TL_Company.Where(t => t.CompanyCode == obj.CompanyCode).FirstOrDefault();
                if (n != null)
                {
                    n.CompanyLogo = fn;
                    dc.SubmitChanges();
                }
                fu.SaveAs(path);
                img.ImageUrl = path;                
            }
            else
            {
                Response.Write("<script>alert('错误的图片文件格式');history.back();</script>");
            }
        }

        protected void abtnuploadblogo_Click(object sender, EventArgs e)
        {
            TL_Company obj = this.ASPxGridView1.GetRow(this.ASPxGridView1.EditingRowVisibleIndex) as TL_Company;
            FileUpload fu = (FileUpload)this.ASPxGridView1.FindEditFormTemplateControl("FileUploadb");
            Image img = (Image)this.ASPxGridView1.FindEditFormTemplateControl("imgb");
            string fullFileName = fu.PostedFile.FileName;
            string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
            string typeFileName = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
            if (typeFileName == "jpg" || typeFileName == "gif" || typeFileName == "png" || typeFileName == "bmp")
            {
                string fn = obj.CompanyCode.ToString() + "." + typeFileName;
                string path = Server.MapPath(@"..\Images\Brand") + "\\" + fn;
                System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath(@"..\Images\Brand") + "\\" + obj.BrandingLogo);
                if (fi.Exists)
                {
                    fi.Delete();
                }
                obj.BrandingLogo = fn;
                var dc = DataContextFactory.Create();
                var n = dc.TL_Company.Where(t => t.CompanyCode == obj.CompanyCode).FirstOrDefault();
                if (n != null)
                {
                    n.BrandingLogo = fn;
                    dc.SubmitChanges();
                }
                fu.SaveAs(path);
                img.ImageUrl = path;
            }
            else
            {
                Response.Write("<script>alert('错误的图片文件格式');history.back();</script>");
            }
        }

        protected void abtnExport_Click(object sender, EventArgs e)
        {
            this.ASPxGridViewExporter1.WriteXlsToResponse(true);
        }
    }
}